package hfut.example.filter;

import com.alibaba.fastjson.JSON;
import hfut.example.common.BaseContext;
import hfut.example.common.R;
import hfut.example.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 检查用户是否完成登录
 */
@Slf4j
@WebFilter(filterName="loginCheckFilter",urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //1.获取本次的URI
        String requestURI = request.getRequestURI();

        //不需要处理的请求路径
        String[] urls = {"/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/setmeal/**",
                "/user/sendMsg",
                "/user/login",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"

        };

        //2.判断本次请求是否需要处理
        boolean check = check(urls, requestURI);

        //3.无需处理
        if(check){
            filterChain.doFilter(request,response);
            return;
        }

        //4-1.PC:需要处理,若登录则放行
        if(request.getSession().getAttribute("employee")!=null){
            Long empId= (Long) request.getSession().getAttribute("employee");

            log.info("♥♥♥♥♥♥♥♥♥♥管理{}已登录♥♥♥♥♥♥♥♥♥♥",empId);

            //设置进程Id为当前用户Id
            BaseContext.setCurrentId(empId);

            filterChain.doFilter(request,response);
            return;
        }
        //4-2.移动:需要处理,若登录则放行
        if(request.getSession().getAttribute("user")!=null){
            Long userId= (Long) request.getSession().getAttribute("user");

            log.info("♥♥♥♥♥♥♥♥♥♥用户{}已登录♥♥♥♥♥♥♥♥♥♥",userId);

            //设置进程Id为当前用户Id
            BaseContext.setCurrentId(userId);

            filterChain.doFilter(request,response);
            return;
        }

        //5.未登录则跳转,通过输出流方式
        log.info("拦截到请求:{}",requestURI);
        log.info("♣♣♣♣♣♣♣♣用户未登录♣♣♣♣♣♣♣♣");
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
    }

    /**
     * 检查本次是否需要放行
     * @param urls
     * @param requestURI
     * @return
     */
    public boolean check(String[]urls,String requestURI){
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI);
            if(match){
                return true;
            }
        }
        return false;
    }



}
